package cn.edu.qvtu.controller;

import cn.edu.qvtu.entity.Result;
import cn.edu.qvtu.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @FileName UploadController
 * @Description
 * @Author YooJia Chen
 * @date 2023-07-15
 */

@Slf4j
@RestController
public class UploadController {
//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
//        log.info("文件上传：{},{},{}", username, age, image);
//        // 获取原始文件名
//        String orignalFilename = image.getOriginalFilename();
//
//        // 构造唯一的文件名
//        String extname = orignalFilename.substring(orignalFilename.lastIndexOf("."));
//        String newFileName = UUID.randomUUID().toString() + extname;
//
//        // 将文件存储在服务器的磁盘目录中 E:\Date\image
//        image.transferTo(new File("D:\\Data\\image\\" + newFileName));
//
//        return Result.success();
//    }
    @Autowired
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传，文件名：{}", image.getOriginalFilename());

        String url = aliOSSUtils.upload(image);

        log.info("文件上传完成，url:{}", url);

        return Result.success(url);
    }
}
